<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	public function index()
	{
		// do this later 
	}
	public function login()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->library('encrypt');
		$this->load->model('user');

		$this->form_validation->set_rules('email', 'Email', 'required|callback_auth_user');
		$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_message('auth_user', 'Wrong email or password. ');
		if ($this->form_validation->run() === FALSE)
		{
		
			$this->load->view('users/login');

		}
		else
		{
			$this->user->login($this->input->post('email'), $this->input->post('password'));
			//redirect(site_url('app/index'));
		}
	}
	/******************************************************************************************
	*
	*	Signs the user up for turnup and then signs them in. 
	*
	**********************************************************************************************/
	public function sign_up()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->library('encrypt');
		$this->load->model('user');

		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[12]|matches[password_confirmation]');
		$this->form_validation->set_rules('email', 'Email', 'callback_is_user');
		$this->form_validation->set_message('is_user', 'The email you are trying to register with is already in our system. ');
		if ($this->form_validation->run() === FALSE)
		{
		
			$this->load->view('users/create');
			

		}
		else
		{

			$this->user->create($this->input->post('email'), 
								$this->input->post('name'), 
								$this->input->post('password'));
		//	redirect('/turnup/index.php/app');
			//will probably redirect to app eventually. 
			//$this->load->view('users/show');
		}

	}
	/********************************************************
	*
	*	Tests whether or not user is currently in database
	*
	********************************************************/
	public function is_user($email)
	{
		$this->load->model('user');
		return !$this->user->is_user($email);

	}
	public function auth_user()
	{
		$this->load->model('user');
		$email = $this->input->post('email');
		$password = $this->input->post('password');
		
		return $this->user->login($email, $password);
	}
	/**********************************************
	*
	*	Logs user out of the system. 
	*
	**********************************************/
	public function logout()
	{
		$this->load->model('user');
		$this->user->logout();
		//eventually redirect user somewhere from there. 
	}

	public function show()
	{
		echo $this->session->userdata('user_name');
		echo "<br>";
		echo $this->session->userdata('user_email');
		echo "<br>";
		echo $this->session->userdata('user_level');
		echo "<br>";
		echo var_dump($this->session->userdata);
	}
	/*****************************************************************************
	*
	*	Logs user into facebook. 
	*	
	*
	*****************************************************************************/
	/*public function facebook_login()
	{
		
        $this->load->model('user');
        $config = $this->config->item('facebook');
		$this->load->library('facebook', $config);

        $user = $this->facebook->getUser();
        $this->load->library('encrypt');
        if ($user) 
        {
            if($this->user->is_user($this->facebook->api('/me')['email']))
            {
            	echo "user should be logged";
            	$this->user->set_session($this->facebook->api('/me')['email']);
            }
            else
            {

            	$email = $this->facebook->api('/me')['email'];
            	$name = $this->facebook->api('/me')['name'];
            	$fake_password = substr(md5(microtime()),rand(0,26),8);
            	$this->user->create($email, $name, $fake_password);
            	$this->user->set_session($this->facebook->api('/me')['email']);
            }
        }
     
    }*/
		
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */